package com.chen.product.service;

import com.chen.product.api.UserDto;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: SpringCloud-parent
 * @description: 在Feign中使用hystrix回退
 * @author: Mr.Chen
 * @create: 2019-04-30 22:02
 **/
@Component
public class UserServiceFallback implements UserServiceFeign {
    /**
     * 实现用户列表查询回退方法
     * @return
     */
    @Override
    public List<UserDto> findAll() {
        return this.findAllFallback();
    }

    /**
     * 实现用户详情获取回退方法。
     * @param id
     * @return
     */
    @Override
    public UserDto load(Long id) {
        return this.loadFallback(id);
    }

    /**
     * 具体用户列表回退方法
     * @return
     */
    protected List<UserDto> findAllFallback() {
        ArrayList<UserDto> userDtos = new ArrayList<>();
        userDtos.add(new UserDto(1L, "zhangSan_static", "/users/avatar/zhangsan.png"));
        userDtos.add(new UserDto(2L, "lisi_static", "/users/avatar/lisi.png"));
        userDtos.add(new UserDto(3L, "wangwu_static", "/users/avatar/wangwu.png"));
        userDtos.add(new UserDto(4L, "yanxiaoliu_static", "/users/avatar/yanxiaoliu.png"));
        return userDtos;
    }

    /**
     * 具体用户详情回退方法
     * @param id
     * @return
     */
    protected UserDto loadFallback(Long id) {
        return new UserDto(id, "Anonymous", "default.png");
    }
}
